<template name="instance_traces" >

    <div class="instance-traces">
        <div class="">
            <h3 class="box-title">{{_t "instance_approval_history"}}</h3>
        </div>
        <div class="">

            <table class='table table-condensed traces'>
                <tbody>

                {{#each this}}
                    {{#if firstTrace @index}}
                        {{#with last_distribute_from this.instance}}
                            <tr class="trace-item">
                                <td colspan='3' class="trace-step-name">{{_t "instance_distribute_title"}}</td>
                            </tr>
                            <tr class="approve-item-distribute">
                                <td class="name">
                                    {{#if isMobile}}
                                        {{this.from_user_name}}
                                    {{else}}
                                        <a class="btn-link" href="{{objectUrl 'users' this.from_user '-'}}" data-target_user_id="{{this.from_user}}">
                                            {{this.from_user_name}}
                                        </a>
                                    {{/if}}
                                </td>
                                <td class="finish-date">{{dateFormat this.created}}</td>
                            </tr>
                        {{/with}}
                    {{/if}}

                    <tr class="trace-item">
                        <td colspan='3' class = "trace-step-name">{{getStepName this.step}}</td>
                    </tr>
                    {{#each this.approves}}
                            <tr class="approve-item">
                                <td class="name">
                                    {{#if isMobile}}
                                        {{this.handler_name}}
                                    {{else}}
                                        <a class="btn-link" href="{{objectUrl 'users' this.handler '-'}}" data-target_user_id="{{this.handler}}">
                                            {{this.handler_name}}
                                        </a>
                                    {{/if}}
                                    {{#if isCC this}}
                                        <label class='cc-label'>
                                            <span class="cc-label-title">({{_t "instance_cc_title"}})</span>
                                        </label>
                                    {{/if}}
                                    {{#if isForward this}}
                                        <label class='cc-label'>
                                            <span class="forward-label-title">({{_t "instance_forward_title"}})</span>
                                        </label>
                                    {{/if}}
                                    {{#if isDistribute this}}
                                        <label class='cc-label'>
                                            <span class="forward-label-title">({{_t "instance_distribute_title"}})</span>
                                        </label>
                                    {{/if}}
                                </td>
                                {{#if unempty this.finish_date}}
                                    {{#if isDistribute this}}
                                        {{#if judgeTerminated this.judge}}
                                            <td class="finish-date"><span class='{{getApproveStatusIcon this.judge}}'></span>&nbsp;&nbsp;<span>{{getApproveStatusText this.judge}}</span></td>
                                        {{else}}
                                            <td class="finish-date"><span class="{{getInstanceStateColor this.forward_instance}}">{{getInstanceStateText this.forward_instance}}</span></td>
                                        {{/if}}
                                        <td class="status {{this.judge}}"></td>
                                    {{else}}
                                        <td class="finish-date">{{dateFormat this.finish_date}}</td>
                                        <td class="status {{getApproveJudgeClass this.judge this.auto_submitted}}"><span class='{{getApproveStatusIcon this.judge this.auto_submitted}}'></span><span class="hidden-xs">&nbsp;&nbsp;{{getApproveStatusText this.judge this.auto_submitted}}</span></td>
                                    {{/if}}
                                {{else}}
                                    <td class="finish-date">
                                        {{#if this.is_read}}
                                            <span class='blue'>{{_t "instance_approve_read"}}</span>
                                        {{else}}
                                            <span class='red'>{{_t "instance_approve_not_yet_handled"}}</span>
                                        {{/if}}
                                    </td>
                                    {{#if isCC this}}
                                        <td class="status pending"></td>
                                    {{else}}
                                        {{#if isForward this}}
                                            <td class="status pending"></td>
                                        {{else}}
                                            <td class="status pending"><span class='ion ion-ios-loop'></span><span class="hidden-xs">&nbsp;&nbsp;{{_t "instance_in_progress"}}</span></td>
                                        {{/if}}
                                    {{/if}}
                                {{/if}}
                            </tr>
                            {{#if this.is_finished}}
                                {{#if unempty this.description}}
                                    <tr class="approve-description">
                                        <td class="description" colspan="3">{{{markDownToHtml this.description}}}</td>
                                    </tr>
                                {{/if}}
                            {{else}}
                                {{#if (unempty (myApproveDescription this._id))}}
                                    <tr class="approve-description">
                                        <td class="description" colspan="3">{{{markDownToHtml (myApproveDescription this._id)}}}</td>
                                    </tr>
                                {{/if}}
                            {{/if}}
                            {{#if this.agent}}
                                <tr class="approve-description">
                                    <td class="description" colspan="3">{{{agentDescription this.user_name}}}</td>
                                </tr>
                            {{/if}}
                    {{/each}}
                {{/each}}
                </tbody>
            </table>
        </div>
    </div>

</template>
